local wen__kongshang = fk.CreateSkill {
  name = "wen__kongshang",
}

Fk:loadTranslationTable{
  ["wen__kongshang"] = "控熵",
  [":wen__kongshang"] = "当你受到伤害后，你可以选择一名角色并选择一项令其执行之：1.摸一张牌；2.弃置一张手牌。然后若你的手牌数与体力值相等，你可以弃置一张牌，移动场上一张牌。",

  ["wen__kongshang_draw"] = "令其摸一张牌",
  ["wen__kongshang_discard"] = "令其弃置一张手牌",
  ["wen__kongshang_extra"] = "弃置一张牌，移动场上一张牌",
  ["#wen__kongshang-choose"] = "控熵：请选择一名角色，并选择一项令其执行",
  ["#wen__kongshang-move"] = "控熵：请选择两名角色，移动其场上的一张牌",

  ["$wen__kongshang1"] = "万物皆有序，唯熵增不可逆。",
  ["$wen__kongshang2"] = "以我之力，掌控混沌。",
}

wen__kongshang:addEffect(fk.Damaged, {
  anim_type = "masochism",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(wen__kongshang.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room.alive_players,
      skill_name = wen__kongshang.name,
      prompt = "#wen__kongshang-choose",
      cancelable = true,
    })
    if #to > 0 then
      local choice = room:askToChoice(player, {
        choices = {"wen__kongshang_draw", "wen__kongshang_discard"},
        skill_name = wen__kongshang.name,
      })
      if choice then
        event:setCostData(self, {to = to[1], choice = choice})
        return true
      end
    end
    return false
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local costData = event:getCostData(self)
    local to = costData.to
    local choice = costData.choice
    
    player:broadcastSkillInvoke(wen__kongshang.name)
    
    if choice == "wen__kongshang_draw" then
      to:drawCards(1, wen__kongshang.name)
    else
      room:askToDiscard(to, {
        skill_name = wen__kongshang.name,
        min_num = 1,
        max_num = 1,
        include_equip = false,
        cancelable = false,
      })
    end
    
    if player:getHandcardNum() == player.hp then
      local extra_choice = room:askToChoice(player, {
        choices = {"wen__kongshang_extra", "Cancel"},
        skill_name = wen__kongshang.name,
        prompt = "控熵：你的手牌数与体力值相等，是否弃置一张牌并移动场上一张牌？",
      })
      
      if extra_choice == "wen__kongshang_extra" then
        if #player:getCardIds("he") > 0 then
          room:askToDiscard(player, {
            skill_name = wen__kongshang.name,
            min_num = 1,
            max_num = 1,
            include_equip = true,
            cancelable = false,
          })
        end
        
        if #room:canMoveCardInBoard() > 0 then
          local targets = room:askToChooseToMoveCardInBoard(player, {
            prompt = "#wen__kongshang-move",
            skill_name = wen__kongshang.name,
          })
          if #targets == 2 then
            room:askToMoveCardInBoard(player, {
              target_one = targets[1],
              target_two = targets[2],
              skill_name = wen__kongshang.name,
            })
          end
        end
      end
    end
  end,
})

return wen__kongshang